package com.snowj.apaas.web;


import com.snowj.apaas.domain.auto.Agreements;
import com.snowj.apaas.service.AgreementService;
import com.snowj.apaas.service.utils.PaginationUtil;
import com.snowj.apaas.service.utils.ResponseUtil;
import com.snowj.apaas.web.vm.AgreementsVm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Api(value = "合同管理", tags = {"合同管理接口"})
@RestController
@RequestMapping("/api")
public class AgreementsController {

    private final AgreementService agreementService;

    public AgreementsController(AgreementService agreementService) {
        this.agreementService = agreementService;
    }

    /**
     * 列出合同列表
     * 签约日期，客户名称，项目内容，商务人员，工程师，进度
     *
     * @param pageable
     * @return
     */
    @ApiOperation("获取合同列表")
    @GetMapping("/agreements")
    public ResponseEntity<List<AgreementsVm>> listAgreements(String startDate, String endDate, String clientName, String projectInfo,
                                                             String businessPeople, String engineer, Integer process, Pageable pageable) {
        Page<AgreementsVm> page = agreementService.listAgreements(startDate, endDate, clientName, projectInfo, businessPeople, engineer, process, pageable);
        HttpHeaders httpHeaders = PaginationUtil.generatePaginationHttpHeaders(page);
        return ResponseEntity.status(HttpStatus.OK)
                .headers(httpHeaders)
                .body(page.getContent());
    }

    /**
     * 根据编号查询合同
     *
     * @param id 合同编号
     * @return
     */
    @ApiOperation("获取单个合同信息")
    @GetMapping("/agreements/{id}")
    public ResponseEntity<Agreements> findAgreementsById(@PathVariable Long id) {
        Optional<Agreements> agreements = agreementService.findAgreementsById(id);
        return ResponseUtil.wrapOrNotFound(agreements);
    }

    /**
     * 创建合同
     *
     * @param agreements 合同信息
     * @return
     */
    @ApiOperation("创建合同")
    @PostMapping("/agreements")
    public ResponseEntity<Agreements> createAgreements(@RequestBody Agreements agreements) {
        Agreements agreement = agreementService.createAgreements(agreements);
        return ResponseEntity.ok(agreement);
    }

    /**
     * 更新合同
     *
     * @param agreements 合同信息
     * @return
     */
    @ApiOperation("更新合同")
    @PutMapping("/agreements")
    public ResponseEntity<Agreements> updateAgreements(@RequestBody Agreements agreements) {
        Agreements agreement = agreementService.updateAgreements(agreements);
        return ResponseEntity.ok(agreement);
    }

    /**
     * 删除合同
     *
     * @param id 合同编号
     * @return
     */
    @ApiOperation("删除合同")
    @DeleteMapping("/agreements/{id}")
    public ResponseEntity<Void> deleteAgreements(@PathVariable Long id) {
        agreementService.deleteAgreements(id);
        return ResponseEntity.noContent().build();
    }
}
